//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO.Packaging;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class ExternalData : IOfficeXmlSerializable
    {
        private bool? autoUpdate;
        private string extervalValue;
        private string RelationshipType;
        private const string xmlName = "externalData";

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read(xmlName))
            {
                string rid = c.GetString(OfficeConstants.RelationshipNamespace + "id");
                var rel = c.Part.GetRelationship(rid);
                this.extervalValue = rel.TargetUri.ToString();
                this.RelationshipType = rel.RelationshipType;
                this.autoUpdate = c.ReadOptionalElementBooleanAttribute("autoUpdate");
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (var c = context.Write(xmlName))
            {
                string s = extervalValue;// = Uri.EscapeUriString(extervalValue);
                //s = extervalValue.Replace(" ", "%20").Replace("{", "%7b").Replace("{", "%7b").Replace("}", "%7d");

                var rel = c.CreateExternalRelationship(s,
                                          RelationshipType);
                c.SetString(OfficeConstants.RelationshipNamespace + "id", rel.Id);
                c.WriteOptionalElementBooleanAttribute("autoUpdate", this.autoUpdate);
            }
        }

        #endregion
    }
}